<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Houseme extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
        $this->load->config('layout', TRUE);
		$this->load->helper(array('form', 'url'));
		$this->load->library('security');
		$this->load->library('houseme_view');
		$this->load->library('facebook');
		$this->load->library('tank_auth');
		$this->load->database();
		$this->load->model('post_model');
		$this->load->model('topic_model');
		$this->load->model('user_model');
		$this->lang->load('backyard');
		$this->lang->load('houseme');
		//echo "<pre>123:";print_r($this->session->all_userdata());echo "</pre>";exit;
	}

	function index()
	{
		//$user_data = $this->session->all_userdata();//get session
		//echo "<pre>";print_r($user_data);echo "</pre>";exit;
		/*check FB login status & get user data*/
		$get_data = $this->input->get(NULL, TRUE); // returns all GET items with XSS filter
        if (isset($get_data['code'])) {
            $user = $this->facebook->login();
        }
		else $user = array();
		
		$user_data = $this->session->userdata('user_data');//get session

		$data = array();
		$offset = $this->config->item('more_photo_offset', 'layout');
		$data['page_next_link'] = '/houseme/get_more_photo/'.$offset.'/'.$offset;
		$data['neweast_share'] = $this->post_model->get_neweast_share();
		$data['all_post'] = $this->post_model->get_post_by_offset(0,$offset);//$this->post_model->get_all_post();
		$data['all_topic'] = $this->topic_model->get_topic_by_offset(0,$offset);
		$data['all_post'] = array_merge($data['all_post'],$data['all_topic']);

		usort($data['all_post'], array('Houseme','dateSort'));//sort post and topic array by datetime
		$data['category'] = $this->config->item('category', 'layout');
		$data['login_area'] = $this->load->view('houseme/login_area', '', true);
		//echo $data['login_area'];exit;
		$this->load->view('houseme/index', $data);
	}
	function my(){
		if($this->facebook->checkLoginStatus()){
			$data = array();
			$user_data = $this->session->all_userdata();//get session
			
			$uid = $user_data['user_data']['LOGIN_INFO']['id'];
			$data['uid'] = $uid;
			$data['category'] = $this->config->item('category', 'layout');
			$data['login_area'] = $this->load->view('houseme/login_area', '', true);
			$data['collect_board_url'] = '/collect/'.$uid.'/';
			$user_collected = array();
			$user_shared = array();
			$user_collected_alias = array();
			$data['user_collected'] = array();
			$data['user_collected_alias'] = array();
			$data['user_name'] = !empty($user_data['user_data']['LOGIN_INFO']['name'])?$user_data['user_data']['LOGIN_INFO']['name']:$this->lang->line('username_not_set');

			// get user collects
			$user_collects = $this->user_model->selectUserCollects($uid);

			// get user share post
			$user_shares = $this->user_model->selectUserShares($uid);

			
			if(!empty($user_collects)){
				$collects = $user_collects[0]['collects'];
				$collects = urldecode($collects);
				$collects = json_decode($collects);

				$user_collected_alias = $user_collects[0]['collect_alias'];
				$user_collected_alias = urldecode($user_collected_alias);
				$user_collected_alias = json_decode($user_collected_alias);
				//echo "<pre>";print_r($collects);echo "</pre>";exit;
				
				foreach ($collects as $key => $value) {

					if(!empty($value)){
						//initial arrays
						$user_collected[$key] = array();
						$post_collected_id_string = array();
						$share_collected_id_string = array();
						$topic_collected_id_string = array();
						$collected_topic = array();
						$collected_post = array();
						$collected_share = array();

						foreach ($value as $k => $collect_link) {
							if(strpos($collect_link, 'post') !== false){
								$temp = explode('/', $collect_link);
								$post_collected_id_string[] = $temp[3];
							}
							elseif(strpos($collect_link, 'topic') !== false){
								$temp = explode('/', $collect_link);
								$topic_collected_id_string[] = $temp[3];
							}
							elseif(strpos($collect_link, 'share') !== false){
								$temp = explode('/', $collect_link);
								$share_collected_id_string[] = $temp[3];
							}
						}

						
						if(!empty($topic_collected_id_string)){
							$collected_topic = $this->topic_model->get_topic_by_id_string($topic_collected_id_string);
						}

						if(!empty($post_collected_id_string)){
							$collected_post = $this->post_model->get_post_by_id_string($post_collected_id_string);	
						}

						if(!empty($share_collected_id_string)){
							$collected_share = $this->post_model->get_share_by_id_string($share_collected_id_string);	
						}
						
						
						
						$user_collected[$key] = array_merge($collected_post,$collected_topic);
						$user_collected[$key] = array_merge($collected_share,$user_collected[$key]);
						
						usort($user_collected[$key], array('Houseme','dateSort'));//sort post and topic array by datetime
						//echo "<pre>";print_r($user_collected);echo "</pre>";exit;
					}
				}
			}

		
			
			$data['user_collected'] = $user_collected;
			$data['user_collected_alias'] = $user_collected_alias;
			$data['user_shared'] = $user_shares;
			//echo "<pre>";print_r($data);echo "</pre>";exit;

			$this->load->view('houseme/my', $data);
		}
		elseif($this->tank_auth->is_fe_logged_in()){
			$data = array();
			$user_data = $this->session->all_userdata();//get session
			$uid = $user_data['fe_user_id'];
			$data['uid'] = $uid;
			$data['category'] = $this->config->item('category', 'layout');
			$data['login_area'] = $this->load->view('houseme/login_area', '', true);
			$data['collect_board_url'] = '/collect/'.$uid.'/';
			$user_collected = array();
			$user_collected_alias = array();
			$data['user_collected'] = array();
			$data['user_collected_alias'] = array();
			$data['user_name'] = !empty($user_data['fe_username'])?$user_data['fe_username']:$this->lang->line('username_not_set');
			// get user collects
			$user_collects = $this->user_model->selectUserCollects($uid);
			// get user share post
			$user_shares = $this->user_model->selectUserShares($uid);
			
			if(!empty($user_collects)){
				$collects = $user_collects[0]['collects'];
				$collects = urldecode($collects);
				$collects = json_decode($collects);

				$user_collected_alias = $user_collects[0]['collect_alias'];
				$user_collected_alias = urldecode($user_collected_alias);
				$user_collected_alias = json_decode($user_collected_alias);
				//echo "<pre>";print_r($collects);echo "</pre>";exit;
				
				foreach ($collects as $key => $value) {

					if(!empty($value)){
						//initial arrays
						$user_collected[$key] = array();
						$post_collected_id_string = array();
						$share_collected_id_string = array();
						$topic_collected_id_string = array();
						$collected_topic = array();
						$collected_post = array();
						$collected_share = array();

						foreach ($value as $k => $collect_link) {
							if(strpos($collect_link, 'post') !== false){
								$temp = explode('/', $collect_link);
								$post_collected_id_string[] = $temp[3];
							}
							elseif(strpos($collect_link, 'share') !== false){
								$temp = explode('/', $collect_link);
								$share_collected_id_string[] = $temp[3];
							}
							elseif(strpos($collect_link, 'topic') !== false){
								$temp = explode('/', $collect_link);
								$topic_collected_id_string[] = $temp[3];
							}
						}

						
						if(!empty($topic_collected_id_string)){
							$collected_topic = $this->topic_model->get_topic_by_id_string($topic_collected_id_string);
						}

						if(!empty($post_collected_id_string)){
							$collected_post = $this->post_model->get_post_by_id_string($post_collected_id_string);	
						}

						if(!empty($share_collected_id_string)){
							$collected_share = $this->post_model->get_share_by_id_string($share_collected_id_string);	
						}
						
						
						
						$user_collected[$key] = array_merge($collected_post,$collected_topic);
						$user_collected[$key] = array_merge($collected_share,$user_collected[$key]);
						
						usort($user_collected[$key], array('Houseme','dateSort'));//sort post and topic array by datetime
						//echo "<pre>";print_r($user_collected);echo "</pre>";exit;
					}
				}
			}
			
			
			$data['user_collected'] = $user_collected;
			$data['user_collected_alias'] = $user_collected_alias;
			$data['user_shared'] = $user_shares;
			//echo "<pre>";print_r($data);echo "</pre>";exit;

			$this->load->view('houseme/my', $data);
		}
		else{
			echo "<script>";
				echo "alert('please login first');window.location.href = '/';";
			echo "</script>";
			//redirect('/houseme/FBlogin/');
		}
		
	}

	function check_my_status(){
		$return = array();
		if($this->facebook->checkLoginStatus() || $this->tank_auth->is_fe_logged_in()){
			$return['status_code'] = '1'; // login
			$return['mylink'] = '/my';
		}
		else{
			$return['status_code'] = '2'; // not login
			$return['msg'] = '請先登入';
		}

		echo json_encode($return);exit;
	}

	function search(){
		$data = $this->input->get(); // returns all GET items
		//echo "<pre>";print_r($data);echo "</pre>";exit;
		$keyword = urldecode($data['q']);
		$offset = $this->config->item('more_photo_offset', 'layout');
		$data['page_next_link'] = '/houseme/get_more_photo/'.$offset.'/'.$offset.'/'.urlencode($data['q']);
		$data['all_post'] = $this->post_model->get_post_by_keyword($keyword,0,$offset);
		$data['all_topic'] = $this->topic_model->get_topic_by_keyword($keyword,0,$offset);
		$data['all_share'] = $this->post_model->get_share_by_keyword($keyword,0,$offset);
		$data['all_post'] = array_merge($data['all_post'],$data['all_topic']);
		$data['all_post'] = array_merge($data['all_post'],$data['all_share']);
		usort($data['all_post'], array('Houseme','dateSort'));//sort post and topic array by datetime
		$data['category'] = $this->config->item('category', 'layout');
		$data['keyword'] = $keyword;
		$data['login_area'] = $this->load->view('houseme/login_area', '', true);
		$this->load->view('houseme/index', $data);

	}

	function topics(){
		$data = $this->input->get(); // returns all GET items
		//echo "<pre>";print_r($data);echo "</pre>";exit;
		
		$data['all_topic'] = $this->topic_model->get_all_topic();
		$data['category'] = $this->config->item('category', 'layout');
		$data['login_area'] = $this->load->view('houseme/login_area', '', true);
		$this->load->view('houseme/topic_index', $data);
	}

	function get_more_photo(){
		//sleep(1);
		$offset = $this->uri->segment(3);
		$number = $this->uri->segment(4);
		$keyword = $this->uri->segment(5);
		if(!empty($keyword)){
			$keyword = urldecode($keyword);
			$data['keyword'] = $keyword; // used for post navigation
			$data['all_post'] = $this->post_model->get_post_by_keyword($keyword,$offset,$number);
			$data['all_topic'] = $this->topic_model->get_topic_by_keyword($keyword,$offset,$number);
			$data['all_post'] = array_merge($data['all_post'],$data['all_topic']);
			usort($data['all_post'], array('Houseme','dateSort'));//sort post and topic array by datetime
		}
		else{
			$data['all_post'] = $this->post_model->get_post_by_offset($offset,$number);	
			$data['all_topic'] = $this->topic_model->get_topic_by_offset($offset,$number);
			$data['all_post'] = array_merge($data['all_post'],$data['all_topic']);
			usort($data['all_post'], array('Houseme','dateSort'));//sort post and topic array by datetime
		}

		
		$json_array = array();
		if(!empty($data['all_post'])){
			$more_photo = $this->load->view('houseme/more_photo', $data,true);
			if(!empty($keyword)){
				
				$page_next_link = '/houseme/get_more_photo/'.($offset+$number).'/'.$number.'/'.urlencode($keyword);
			}
			else{
				$page_next_link = '/houseme/get_more_photo/'.($offset+$number).'/'.$number;	
			}
			
			$json_array = array(
				'nextLink'	=>	$page_next_link,
				'html'	=>	$more_photo
			);
		}
		

		echo json_encode($json_array);
	}

	function get_more_category_photo(){
		//sleep(1);
		$offset = $this->uri->segment(3);
		$number = $this->uri->segment(4);
		$category_id = $this->uri->segment(5);

		$data['category'] = $this->config->item('category', 'layout');
        $category_string = $data['category'][$category_id];

        $data['all_post'] = $this->post_model->get_all_post_by_category_string($category_string,$offset,$number);
        $data['category_id'] = $category_id;
		
		$json_array = array();
		if(!empty($data['all_post'])){
			$more_photo = $this->load->view('houseme/more_photo', $data,true);
			$page_next_link = '/houseme/get_more_category_photo/'.($offset+$number).'/'.$number.'/'.$category_id;
			
			$json_array = array(
				'nextLink'	=>	$page_next_link,
				'html'	=>	$more_photo
			);
		}
		

		echo json_encode($json_array);
	}

	function showLoginForm(){
		$data = array();
		$return['html'] = $this->load->view('houseme/login_form', $data, true);

		echo json_encode($return);
	}

	function showLoginFirstForm(){
		$data = array();
		$return['html'] = $this->load->view('houseme/login_first', $data, true);

		echo json_encode($return);
	}

	function showRegisterForm(){
		$data = array();
		$return['html'] = $this->load->view('houseme/register_form', $data, true);

		echo json_encode($return);	
	}

	function showForgotPasswordForm(){
		$data = array();
		$return['html'] = $this->load->view('houseme/forgot_password_form', $data, true);

		echo json_encode($return);	
	}

	function about(){
		$data['category'] = $this->config->item('category', 'layout');
		$data['login_area'] = $this->load->view('houseme/login_area', '', true);
		$this->load->view('houseme/abouthouseme', $data);
	}

	function terms(){
		$data['category'] = $this->config->item('category', 'layout');
		$data['login_area'] = $this->load->view('houseme/login_area', '', true);
		$this->load->view('houseme/housemeterms', $data);
	}

	function FBlogin(){
		$user = $this->facebook->login();
	}

	function FBlogout(){
		$user = $this->facebook->logout();
	}

	function dateSort($a,$b){
		$dateA = strtotime($a['modified_time']);
		$dateB = strtotime($b['modified_time']);
		return (($dateA-$dateB)>0)?-1:1;
	}
}

/* End of file houseme.php */
/* Location: ./application/controllers/houseme.php */